//
//  ByIconChooseDataSource.m
//  jmsfhz_ios
//
//  Created by pro on 16/7/18.
//  Copyright © 2016年 pro. All rights reserved.
//

#import "ByIconChooseDataSource.h"

@implementation ByIconChooseDataSource

- (void)fireWithViewController:(UIViewController *)vc {
    if (vc) {
        self.vc = vc;
    }
}


- (void)makeConfigToSelectIcon {
    
    // 适配版本iOS8.0以上
    if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(8)) {
        // 创建ActionSheet
        NSString *albumButtonTitle = @"从相册中选择";
        NSString *cameraButtonTitle = @"拍照";
        NSString *cancelButtonTitle = @"取消";
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
        
        // 添加点击动作
        // 取消
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancelButtonTitle style:UIAlertActionStyleCancel handler:nil];
        // 相册中
        UIAlertAction *albumAction = [UIAlertAction actionWithTitle:albumButtonTitle style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action)
                                      {
                                          [self _choosePhotoFromAlbum];
                                      }];
        // 拍照
        UIAlertAction *cameraAction = [UIAlertAction actionWithTitle:cameraButtonTitle style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action)
                                       {
                                           [self _getphotoFromCamera];
                                       }];
        
        // Add the actions.
        [alertController addAction:cancelAction];
        [alertController addAction:albumAction];
        [alertController addAction:cameraAction];

        [self.vc presentViewController:alertController animated:YES completion:nil];
    }
    // 适配版本8.0以下
    else {
        UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"从相册中选择",@"拍照", nil];
        [sheet showInView:self.vc.view];
    }
}
#pragma mark - UIActionSheet代理方法（适配iOS8.0以下时使用）
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    // 从相册中选择
    if (buttonIndex == 0) {
        [self _choosePhotoFromAlbum];
    }
    // 通过拍照获取
    else if (buttonIndex == 1) {
        [self _getphotoFromCamera];
    }
}
#pragma mark - 头像图片选择相关操作
// 从相册中选择照片
- (void)_choosePhotoFromAlbum {
    UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    if (![UIImagePickerController isSourceTypeAvailable:sourceType]) {
        
        [ByCoreToolAlert showFailureWithText:@"相册不可用!"];
        return;
    }
    [self _changeToPickerController:sourceType];
}

// 通过拍照获取照片
- (void)_getphotoFromCamera {
    
    UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;
    if (![UIImagePickerController isSourceTypeAvailable:sourceType]) {
        
        [ByCoreToolAlert showFailureWithText:@"相册不可用!"];
        return;
    }
    [self _changeToPickerController:sourceType];
}


// 创建并跳转到图片获取界面
- (void)_changeToPickerController:(UIImagePickerControllerSourceType)sourceType {
    // 创建图片拾取界面
    UIImagePickerController *pickerController = [[UIImagePickerController alloc] init];
    pickerController.delegate = self;
    // 设置可编辑
    pickerController.allowsEditing = YES;
    pickerController.sourceType = sourceType;
    pickerController.title = @"头像选择";
    [pickerController.navigationBar setTintColor:BY_THEME_COLOR];
    pickerController.navigationBar.translucent = false;
    [self.vc presentViewController:pickerController animated:YES completion:nil];
}

// UIImagePickerController代理方法，选取图片之后处理
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {
    
    UIImage *editedImage = (UIImage *)[info objectForKey:UIImagePickerControllerEditedImage];
    UIImage *originalImage = (UIImage *)[info objectForKey:UIImagePickerControllerOriginalImage];
    if (editedImage) {
        // 保存到相册
        UIImageWriteToSavedPhotosAlbum(editedImage, nil, nil, nil);
        // 返回“个人”界面
        [self.vc dismissViewControllerAnimated:YES completion:^{
            if (self.completeBlock) {
                self.completeBlock(editedImage);
            }
        }];

    }
    else if (originalImage) {
        // 返回“个人”界面
        [self.vc dismissViewControllerAnimated:YES completion:^{
            if (self.completeBlock) {
                self.completeBlock(originalImage);
            }
        }];

    }
}




@end
